package com.xtsoft.platform.task.controller;

import com.fasterxml.jackson.databind.JsonMappingException;

import com.xtsoft.core.common.YesNo;
import com.xtsoft.core.controller.GenericController;
import com.xtsoft.core.repository.QueryFilter;
import com.xtsoft.core.repository.QueryResult;
import com.xtsoft.core.repository.Sort;
import com.xtsoft.core.utils.ResultMsg;
import com.xtsoft.core.utils.StringUtils;
import com.xtsoft.core.utils.UUIDGenerator;

import com.xtsoft.platform.base.system.domain.SysDictionary;
import com.xtsoft.platform.base.system.domain.SysUser;
import com.xtsoft.platform.base.system.service.SysDictionaryService;

import com.xtsoft.platform.base.system.service.SysOrganizeService;
import com.xtsoft.platform.base.system.service.SysUserService;
import com.xtsoft.platform.base.system.service.UserRoleService;
import com.xtsoft.platform.globe.Constants.Constants;
import com.xtsoft.platform.globe.enums.TaskBugType;
import com.xtsoft.platform.globe.utils.SecurityUtils;

import com.xtsoft.platform.task.domain.InfTask;
import com.xtsoft.platform.task.domain.TaskCalend;
import com.xtsoft.platform.task.domain.TaskComment;
import com.xtsoft.platform.task.service.InfTaskService;
import com.xtsoft.platform.task.service.TaskCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParseException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.*;

import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.getUser;

/**
 * 
 * @ClassName: InfTask
 *             <p>
 *             任务处理类
 *             </p>
 * @author: Codegen-代码生成器
 * @date: 2019年12月3日 上午10:59:46
 */
@Tag(name ="任务--处理接口" )
@RestController
public class InfTaskController extends GenericController {

	private final Logger log = LoggerFactory.getLogger(InfTaskController.class);

	@Autowired
	private InfTaskService infTaskService;

	@Autowired
	private TaskCommentService taskCommentService;
    @Autowired
    private SysOrganizeService sysOrganizeService;
    @Autowired
    private SysUserService sysUserService;
	@Autowired
	private SysDictionaryService sysDictionaryService;

	/*
	 * @Autowired private HttpServletRequest request;
	 */

	/**
	 * <p>
	 * 新增一个任务
	 * </p>
	 * 
	 * @param infTask 要新增的任务
	 * @return ResponseEntity<ResultMsg>, ResultMsg的Data中包含 任务
	 * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */
	@PostMapping("/infTask/add")
	@Parameter(name = "SysDictionary", description = "要新增的任务", required = false)
	@Operation(summary ="新增，根据前台传递的任务新增")
	public ResponseEntity<ResultMsg> add(
			@RequestBody InfTask infTask)
			throws URISyntaxException {
		if (null == infTask) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "新增任务失败，传递的任务为空"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("新增项目/任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		if (!StringUtils.isEmpty(infTask.getInfTaskId())) {
			log.info("新增项目/任务失败，新项目/任务不能具有InfTaskId属性值", user.getLoginId(),
					user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "新增项目/任务失败，新项目/任务不能具有InfTaskId属性值"), HttpStatus.OK);
		}
		if(infTask.getIsProject()==YesNo.No.getIntValue()&&infTask.getParentNodeId().equals(Constants.RootNode)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "上级任务/项目不能是项目列表！","上级任务/项目不能是项目列表！"), HttpStatus.OK);
		}
		//如果是项目 改了，乱改的，还没看
		if(infTask.getIsProject()==YesNo.Yes.getIntValue()) {
			infTask.setOrganizeId(sysOrganizeService.getByNodeId(infTaskService.sysOrganize().getNodeId()).getOrganizeId());
		}

		
		ResultMsg resultMag = infTaskService.addTask(infTask, user);
		if (!resultMag.isSuccess()) {
			log.info("新增项目/任务失败");
			resultMag.setMsg("新增项目/任务失败");
			return new ResponseEntity<ResultMsg>(resultMag, HttpStatus.OK);
		}
		log.info("新增项目/任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		resultMag.setMsg("新增项目/任务成功");
		return new ResponseEntity<ResultMsg>(resultMag, HttpStatus.OK);
	}

	/**
	 * <p>
	 * 更新一个任务
	 * </p>
	 * 
	 * @param infTask 要更新的任务
	 * @return ResponseEntity<ResultMsg>, json格式
	 * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
	 * @author Codegen-代码生成器
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @date 2020-07-16 12:10:57
	 */
	@Operation(summary ="更新，根据传递的任务更新实体")
	@Parameter(name = "SysDictionary", description = "要更新的任务", required = false)
	@PostMapping("/infTask/update")


	public ResponseEntity<ResultMsg> update(
			@RequestBody InfTask infTask)
			throws URISyntaxException, IllegalAccessException, InvocationTargetException {
		if (null == infTask) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "更新任务失败，传递的任务为空"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("单条更新任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		// 若 任务无主键值，说明调用错误
		if (StringUtils.isBlank(infTask.getInfTaskId())) {
			log.info("单条更新任务失败，任务对象InfTaskId无属性值", user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "更新失败，任务对象InfTaskId无属性值"), HttpStatus.OK);
		}
		if(infTask.getIsProject()== YesNo.No.getIntValue()&&infTask.getParentNodeId().equals(Constants.RootNode)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "上级任务/项目不能是项目列表！","上级任务/项目不能是项目列表！"), HttpStatus.OK);
		}
		//如果是项目     改了
		if(infTask.getIsProject()==YesNo.Yes.getIntValue()) {
			infTask.setOrganizeId(sysOrganizeService.getByNodeId(infTaskService.sysOrganize().getNodeId()).getOrganizeId());
//			infTask.setSysOrganize(sysOrganizeService.getByNodeId(infTaskService.sysOrganize().getNodeId()));
		}		
		/*
		if(!StringUtils.isBlank(infTask.getSysOrganize().getNodeId())) {
			infTask.setSysOrganize(sysOrganizeService.getByNodeId(infTask.getSysOrganize().getNodeId()));
		}
		*/

		ResultMsg resultMsg=infTaskService.editTask(infTask, user);

		if(!resultMsg.isSuccess()) {
			
			log.info("新增项目/任务成功！", user.getLoginId(),
					user.getUsername(), getClientIp());
			resultMsg.setMsg("编辑项目/任务失败！");
			return new ResponseEntity<ResultMsg>(resultMsg, HttpStatus.OK);
		}
		log.info("单条更新任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(resultMsg,HttpStatus.OK);
	}

	/**
	 * <p>
	 * 根据任务唯一ID值获取任务
	 * </p>
	 * 
	 * @param id 任务唯一ID值
	 * @return ResponseEntity<InfTask>
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */

	@Parameter(name = "id", description ="对象唯一Id", required = false)
	@Operation(summary  = "获取单个任务，若根据任务唯一Id获取到了多个对象则发生异常")
	@GetMapping("/infTask/getByNodeId/{id}")

	public ResponseEntity<ResultMsg> getByNodeId(
		@PathVariable String id) {
		if (StringUtils.isBlank(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，未给定任务的唯一标识"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		InfTask infTask = infTaskService.findByNodeId(id).get(0);
		/*
		 * <p> ofNullable执行逻辑：如果对象为空，则执行orElse，否则执行.map... return
		 * Optional.ofNullable(sysRole).map(result -> new ResponseEntity<>(result,
		 * HttpStatus.OK)) .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
		 */

		if (null == infTask) {
			log.info("获取任务失败", user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，给定的任务可能被删除"), HttpStatus.OK);
		}
		log.info("获取任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取成功", infTask), HttpStatus.OK);
	}

	/**
	 * <p>
	 * 查询任务列表，若分页，则页码信息在Request的参数中，包括当前页，每页记录数
	 * </p>
	 * 
	 * @return ResponseEntity<QueryResult<InfTask>>，json格式
	 * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */
	@GetMapping("/infTask/getAll")
	@Operation(summary = "查询，根据传递的条件查询任务列表，条件参数在Request中")
	public ResponseEntity<QueryResult<InfTask>> getAll()
			throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务列表开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		QueryFilter<InfTask> filter = new QueryFilter<InfTask>(InfTask.class);
		filter.addFilterFromRequest(request);
		//filter.addSort("nodeId", Sort.ASC);
		filter.addSort("updateTime", Sort.DESC);	
		// 判断当前登录的用户是不是超级管理员，如果是超级管理员可以查看所有的任务，如果不是超级管理员只能查看责任人和当前用户相匹配的任务
		if (!sysUserService.isAdmin(user, Constants.AdminRole)) {
			//如果是企业用户
			if(sysUserService.isAdmin(user,Constants.SystemRole)) {
				filter.addFilter("Q_sysOrganize.organizeId_S_EQ", (user.getSysOrganize().getOrganizeId()));
			}else {
				filter.addFilter("Q_userId_S_EQ", (user.getUserId()));
			}
			
		}
		//获取月份或者今日的任务
		String monthOrDay = request.getParameter("monthOrDay");
		if(!StringUtils.isBlank(monthOrDay)) {
			if(monthOrDay.equals(Constants.organizeTree)) {
				//获取当前月份的项目
				infTaskService.getMonth(filter);
			}else if(monthOrDay.equals(Constants.resourceTree)){
				//获取今日的任务
				infTaskService.getDay(filter);
			}else {
				//获取未完成任务
				infTaskService.getIncomplete(filter);
			}
		}
		QueryResult<InfTask> result = infTaskService.anyQuery(filter);



		result.setResultList(infTaskService.userConvert(result.getResultList()));
		log.info("获取任务列表成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<QueryResult<InfTask>>(result, HttpStatus.OK);
	}

	@Operation(summary ="查询，根据day查询任务列表，条件参数在Request中")
	@GetMapping("/infTask/getInfTaskByDay")

	public ResponseEntity<ResultMsg> getInfTaskByDay()
			throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务列表开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		List<InfTask> infTaskList = infTaskService.findInfTaskDayByPlanDate(user.getUserId());
		if (null == infTaskList || infTaskList.size() < 0) {
			log.info("获取任务列表失败", user.getLoginId(), user.getUsername(),
					getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取任务列表失败"), HttpStatus.OK);
		}
		log.info("获取任务列表成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取列表成功", infTaskList), HttpStatus.OK);
	}
	@Operation(summary ="查询，根据month查询任务列表，条件参数在Request中")
	@GetMapping("/infTask/getInfTaskByMonth")

	public ResponseEntity<ResultMsg> getInfTaskByMonth()
			throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务列表开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		List<InfTask> infTaskList = infTaskService.findInfTaskMonthByPlanDate(user.getUserId());
		if (null == infTaskList || infTaskList.size() < 0) {
			log.info("获取任务列表失败", user.getLoginId(), user.getUsername(),
					getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取任务列表失败"), HttpStatus.OK);
		}
		log.info("获取任务列表成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取列表成功", infTaskList), HttpStatus.OK);
	}

	/**
	 * <p>
	 * 根据任务唯一ID值获取任务
	 * </p>
	 * 
	 * @param id 任务唯一ID值
	 * @return ResponseEntity<InfTask>
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */

	@Parameter(name = "id", description="对象唯一Id", required = false)
	@Operation(summary ="获取单个任务，若任务唯一Id获取到了多个对象则发生异常")
	@GetMapping("/infTask/get/{id}")
	public ResponseEntity<ResultMsg> get(
			@PathVariable String id) {
		if (StringUtils.isBlank(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，未给定任务的唯一标识"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		InfTask infTask = infTaskService.get(id);
		/*
		 * <p> ofNullable执行逻辑：如果对象为空，则执行orElse，否则执行.map... return
		 * Optional.ofNullable(sysRole).map(result -> new ResponseEntity<>(result,
		 * HttpStatus.OK)) .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
		 */

		if (null == infTask) {
			log.info("获取任务失败", user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，给定的任务可能被删除"), HttpStatus.OK);
		}
		infTask.setDescript(infTaskService.findDescriptById(infTask.getInfTaskId()));
		log.info("获取任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取成功", infTask), HttpStatus.OK);
	}

	/**
	 * <p>
	 * 根据任务唯一ID值删除单个任务
	 * </p>
	 * 
	 * @param id 任务唯一ID值
	 * @return ResponseEntity,json格式
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */
	@Parameter(name = "id", description="任务唯一Id", required = false)
	@GetMapping("/infTask/delete/{id}")
	@Operation(summary = "删除单个任务")
	public ResponseEntity<ResultMsg> delete(
			 @PathVariable String id) {
		if (StringUtils.isBlank(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "删除单个任务失败，未给定任务的唯一标识"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("删除单条任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		// 查询改节点下面是否有子节点
		List<InfTask> infTaskList = infTaskService.findByParentNodeId(infTaskService.get(id).getNodeId());
		log.info("查询任务子节点开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		if (infTaskList.size() >= 1) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "该节点存在子节点不能删除"), HttpStatus.OK);
		}
		infTaskService.remove(id);
		log.info("删除单条任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "删除成功"), HttpStatus.OK);
	}

	/**
	 * <p>
	 * 根据任务唯一ID值集合，批量删除任务
	 * </p>
	 * 
	 * @param ids 任务唯一ID值集合
	 * @return ResponseEntity<ResultMsg>,返回结果中有删除的记录数，json格式
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */
	@PostMapping("/infTask/deleteBatch")
	@Parameter(name = "ids", description="对象唯一Id", required = false)
	@Operation(summary = "删除任务集合")
	public ResponseEntity<ResultMsg> deleteBatch(
			@RequestParam String ids) {
		if (StringUtils.isBlank(ids)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "批量删除任务失败，未给定任务的唯一标识集合"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("批量删除任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		long rows = infTaskService.deleteBatch(ids);
		log.info("批量删除任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "批量删除任务成功", rows), HttpStatus.OK);
	}

	/**
	 * <p>
	 * 根据任务唯一ID值集合，批量更新任务
	 * </p>
	 * 
	 * @param ids            任务唯一ID值集合
	 * @param properityName  任务要更新的属性名，不带类名
	 * @param properityValue 任务要更新的属性值
	 * @return ResponseEntity<ResultMessage>,返回结果中有更新的记录数，json格式
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */

	@Parameters({@Parameter( name = "properityName", description = "要更新的属性名不带类名，只能是类单个属性名且不能是集合类型", required = false),
			@Parameter(name = "properityValue", description="属性值", required = false),
			@Parameter(name = "ids", description="逗号分隔的企业类型Id集合", required = false)
			})
	@Operation(summary ="新增，根据前台传递的任务新增")
	@PostMapping("/infTask/updateBatch")
	public ResponseEntity<ResultMsg> updateBatch(
			@RequestParam String properityName,
			@RequestParam String properityValue,
			@RequestParam String ids) {
		if (StringUtils.isBlank(ids) || StringUtils.isBlank(properityValue) || StringUtils.isBlank(properityName)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "批量更新任务失败，未给定任务的唯一标识集合或属性名或属性值"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("批量更新任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		long rows = infTaskService.updateBatch(properityName, properityValue, ids);
		log.info("批量更新任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "批量更新任务成功", rows), HttpStatus.OK);
	}

	@GetMapping("/infTask/infTaskCount")
	public ResponseEntity<Map<String, Object>> infTaskCount() {
		// 得到用户
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		String userId = user.getUserId();
		return new ResponseEntity(infTaskService.getInfTaskCount(userId), HttpStatus.OK);
	}

	/**
	 * 
	 * <p>
	 * 完成任务
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @return ResponseEntity<ResultMsg>
	 * @throws @author litao
	 * @date 2021年4月26日
	 * @time 下午3:31:56
	 */
	@Parameter(name = "id", description="对象唯一Id", required = false)
	@Operation(summary ="根据id完成任务")
	@GetMapping("/infTask/perform/{id}")

	public ResponseEntity<ResultMsg> perform(
			@PathVariable String id) {
		if (StringUtils.isEmpty(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "完成任务失败，没有给指定的Id"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("完成任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		InfTask infTask = infTaskService.get(id);
		if (StringUtils.isEmpty(infTask)) {
			log.info("获取任务失败", user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，给定的任务可能被删除"), HttpStatus.OK);
		}
		if(infTaskService.isDuty(user, infTask)) {
			infTaskService.perform(infTask);
		}else {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "只能由当前责任人完成！"), HttpStatus.OK);
		}
				
		log.info("完成任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<>(new ResultMsg(true, "完成任务成功"), HttpStatus.OK);
	}




	@Parameter(name = "id", description="对象唯一Id", required = false)
	@Operation(summary ="查询")
	@GetMapping("/infTask/taskBugCount/{id}")

	public ResponseEntity<ResultMsg> taskBugCount( @PathVariable String id){
		if (StringUtils.isEmpty(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "确定任务失败，没有给指定的Id"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("确定任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		InfTask infTask = infTaskService.get(id);
		if (StringUtils.isEmpty(infTask)) {
			log.info("获取任务失败", user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，给定的任务可能被删除"), HttpStatus.OK);
		}
		infTask.setState(TaskBugType.Determined);
		infTaskService.save(infTask);
		return new ResponseEntity<>(new ResultMsg(true, ""), HttpStatus.OK);
	}

	/**
	 * 
	 * <p>
	 * 终止任务
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @return ResponseEntity<ResultMsg>
	 * @throws @author litao
	 * @date 2021年4月26日
	 * @time 下午3:31:11
	 */

	@Parameter(name = "id", description="对象唯一id", required = false)
	@Operation(summary ="终止任务")
	@GetMapping("/infTask/terminated/{id}")

	public ResponseEntity<ResultMsg> terminated(
			 @PathVariable String id) {
		if (StringUtils.isEmpty(id)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "终止任务失败，没有给指定的Id"), HttpStatus.OK);
		}
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("终止任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		InfTask infTask = infTaskService.get(id);
		if (StringUtils.isEmpty(infTask)) {
			log.info("获取任务失败", user.getLoginId(), user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取单个任务失败，给定的任务可能被删除"), HttpStatus.OK);
		}
		if (!user.getUserId().equals(infTask.getUserId())) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "只能由当前责任人终止！"), HttpStatus.OK);
		}
		infTask.setState(3);
		infTaskService.save(infTask);
		log.info("终止任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		return new ResponseEntity<>(new ResultMsg(true, "终止任务成功"), HttpStatus.OK);
	}

//	/**
//	 * <p>
//	 * 查询任务列表，若分页，则页码信息在Request的参数中，包括当前页，每页记录数
//	 * </p>
//	 * 
//	 * @return ResponseEntity<QueryResult<InfTask>>，json格式
//	 * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
//	 * @author Codegen-代码生成器
//	 * @date 2020-07-16 12:10:57
//	 */
//	@GetMapping("/infTask/getAllByComment")
//	@Timed
//	@ApiOperation(value = "查询，根据传递的条件查询任务列表，条件参数在Request中", httpMethod = "GET")
//	public ResponseEntity<ResultMsg> getAllByComment()
//			throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
//		log.info("获取任务列表开始......", user.getLoginId(), user.getUsername(),
//				getClientIp());
//		List<InfTask> result = infTaskService.getAllInfTaskByTaskComment(request);
//		if (null == result || null == result || result.size() < 0) {
//			log.info("获取任务列表失败", user.getLoginId(), user.getUsername(),
//					getClientIp());
//			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "获取任务列表失败"), HttpStatus.OK);
//		}
//		log.info("获取任务列表成功", user.getLoginId(), user.getUsername(), getClientIp());
//		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取列表成功", infTaskService.getInfTaskUserName(result)),
//				HttpStatus.OK);
//	}

	/**
	 * 
	 * <p>
	 * 任务评价，如果当前任务没有评价则往里添加数据字典的数据
	 * </p>
	 * 
	 * @param id
	 * @return
	 * @return ResponseEntity<ResultMsg>
	 * @throws @author litao
	 * @date 2021年4月26日
	 * @time 下午3:30:55
	 */
	@Parameter(name = "id", description="对象唯一id", required = false)
	@Operation(summary ="任务评价，如果当前任务没有评价则往里添加数据字典的数据")
	@GetMapping("infTask/isComment/{id}")
	public ResponseEntity<ResultMsg> isComment(
		 @PathVariable String id) {
		List<InfTask> infTask = infTaskService.getInfTaskByComment(id);
		// 如果当前任务没有点评则从数据字典里复制过来
		InfTask infTask2 = infTaskService.get(id);
		if (infTask.size() < 1) {
			List<SysDictionary> sysDictionary = sysDictionaryService.getAllSysDictionarieByParaName();
			for (SysDictionary sysDictionary2 : sysDictionary) {
				SysUser user = (SysUser) SecurityUtils.getLoginedUser();
				TaskComment taskComment = new TaskComment();
				taskComment.setInfTask(infTask2);
				taskComment.setState(0);
				taskComment.setUserId(user.getUserId());

				taskComment.setUpdateTime(new Date().getTime());
				taskCommentService.save(taskComment);
			}

		}
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "成功", infTask2), HttpStatus.OK);
	}

	@Parameter(name = "imgFile", required = false)
	@Operation(summary ="任务项目/内容")
	@PostMapping("/infTask/upload")
	public Map<String, Object> upload(@RequestParam(value = "imgFile", required = false) MultipartFile content)
			throws IOException {
		String absolutePath = "";
		if (!content.isEmpty()) {
			byte[] bytes = content.getBytes();
			String uuid = UUIDGenerator.getUUID();
			String fileName = content.getOriginalFilename().replace("\"", "“");
			String path = request.getSession().getServletContext().getRealPath("/") + "/upload/" + uuid;// 绝对路径
			absolutePath = "/upload/" + uuid + "/" + fileName;// 相对路径
			Path dirpath = Paths.get(path);// 文件夹路径
			Path filepath = Paths.get(dirpath + "/" + fileName);// 文件路径
			Files.createDirectories(dirpath);// 创建文件夹
			Files.write(filepath, bytes);// 创建文件
		}
		Map<String, Object> map = new HashMap<>();
		map.put("error", 0);
		map.put("url", absolutePath);
		return map;
	}
	@Operation(summary ="根据projectzero获取任务")
	@GetMapping("infTask/findByIsProjectZero")
	public ResponseEntity<ResultMsg> findByIsProjectZero() {
		List<InfTask> infTaskList = infTaskService.findInfTaskByisProject(YesNo.No.getIntValue());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取任务成功", infTaskList), HttpStatus.OK);
	}
	@GetMapping("infTask/findByIsProjectOne")
	@Operation(summary ="根据projectone获取任务")
	public ResponseEntity<ResultMsg> findByIsProjectOne() {
		List<InfTask> infTaskList = infTaskService.findInfTaskByisProject(YesNo.Yes.getIntValue());
		return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取任务成功", infTaskList), HttpStatus.OK);
	}

	/**
	 * 
	 * <p>
	 * 获取项目/任务
	 * </p>
	 * 
	 * @return
	 * @return ResponseEntity<String>
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 * @throws ParseException
	 * @throws @author              defencez
	 * @date 2021年5月17日
	 * @time 上午7:27:30
	 */
	@GetMapping("/infTask/TaskTree")
	@Operation(summary ="任务树")
	public ResponseEntity<String> TaskTree()
			throws JsonParseException, JsonMappingException, IOException, ParseException {
		String string = "";
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("获取任务列表开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		// 把任务记录查询出来
		QueryFilter<InfTask> taskFilter = new QueryFilter<InfTask>(InfTask.class);
		taskFilter.addFilterFromRequest(request);
		taskFilter.addFilter("Q_isProject_I_EQ", "0");
		QueryResult<InfTask> taskResult = infTaskService.anyQuery(taskFilter);
		// 把项目记录查询出来

		return new ResponseEntity<String>(string, HttpStatus.OK);
	}

	/**
	 * 
	 * <p>
	 * 完成的任务
	 * </p>
	 * 
	 * @return
	 * @return List<TaskCalend>
	 * @throws @author litao
	 * @date 2021年5月17日
	 * @time 下午5:40:12
	 */
	@GetMapping("/infTask/getCalender")
	@Operation(summary ="完成的任务")
	public List<TaskCalend> getCalender() {
		// 如果是超级管理员
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		if (!sysUserService.isAdmin(user,Constants.AdminRole)) {
			List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
			Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserIdTwo();
			lisTaskCalends = list.get("taskCalendList1");
			return lisTaskCalends;
		}
		List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
		Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserId(user.getUserId());
		lisTaskCalends = list.get("taskCalendList1");
		return lisTaskCalends;
	}

	/**
	 * 
	 * <p>
	 * 终止任务
	 * </p>
	 * 
	 * @return
	 * @return List<TaskCalend>
	 * @throws @author litao
	 * @date 2021年5月17日
	 * @time 下午5:40:25
	 */
	@GetMapping("/infTask/getCalender2")
	@Operation(summary ="终止任务")
	public List<TaskCalend> getCalende2r() {
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		if (!sysUserService.isAdmin(user,Constants.AdminRole)) {
			List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
			Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserIdTwo();
			lisTaskCalends = list.get("taskCalendList2");
			return lisTaskCalends;
		}
		List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
		Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserId(user.getUserId());
		lisTaskCalends = list.get("taskCalendList2");
		return lisTaskCalends;
	}

	/**
	 * 
	 * <p>
	 * 未完成任务
	 * </p>
	 * 
	 * @return
	 * @return List<TaskCalend>
	 * @throws @author litao
	 * @date 2021年5月17日
	 * @time 下午5:40:31
	 */
	@GetMapping("/infTask/getCalender3")
	@Operation(summary ="未完成的任务")
	public List<TaskCalend> getCalender3() {
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		if (!sysUserService.isAdmin(user,Constants.AdminRole)) {
			List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
			Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserIdTwo();
			lisTaskCalends = list.get("taskCalendList3");
			return lisTaskCalends;
		}
		List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
		Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserId(user.getUserId());
		lisTaskCalends = list.get("taskCalendList3");
		return lisTaskCalends;
	}

	/**
	 * 
	 * <p>
	 * 即将开始任务
	 * </p>
	 * 
	 * @return
	 * @return List<TaskCalend>
	 * @throws @author litao
	 * @date 2021年5月17日
	 * @time 下午5:40:39
	 */
	@GetMapping("/infTask/getCalender4")
	@Operation(summary ="即将开始的任务")
	public List<TaskCalend> getCalender4() {
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		if (!sysUserService.isAdmin(user,Constants.AdminRole)) {
			List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
			Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserIdTwo();
			lisTaskCalends = list.get("taskCalendList4");
			return lisTaskCalends;
		}
		List<TaskCalend> lisTaskCalends = new ArrayList<TaskCalend>();
		Map<String, List<TaskCalend>> list = infTaskService.findTaskByUserId(user.getUserId());
		lisTaskCalends = list.get("taskCalendList4");
		return lisTaskCalends;
	}
	/**
	 * <p>
	 * 新增一个任务
	 * </p>
	 * 
	 * @param infTask 要新增的任务
	 * @return ResponseEntity<ResultMsg>, ResultMsg的Data中包含 任务
	 * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
	 * @author Codegen-代码生成器
	 * @date 2020-07-16 12:10:57
	 */
	@Parameter(name = "infTask", description="要复制的任务", required = false)
	@PostMapping("/infTask/copy")
	@Operation(summary ="复制，根据前台传递任务新增")
	public ResponseEntity<ResultMsg> copy(
			@RequestBody InfTask infTask)
			throws URISyntaxException {	
		if (null == infTask) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "复制任务失败，传递的任务为空"), HttpStatus.OK);
		}
		infTask.setInfTaskId(null);
		infTask.setNodeId(null);
		SysUser user = (SysUser) SecurityUtils.getLoginedUser();
		log.info("复制项目/任务开始......", user.getLoginId(), user.getUsername(),
				getClientIp());
		if (!StringUtils.isEmpty(infTask.getInfTaskId())) {
			log.info("复制项目/任务失败，新项目/任务不能具有InfTaskId属性值", user.getLoginId(),
					user.getUsername(), getClientIp());
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "复制项目/任务失败，新项目/任务不能具有InfTaskId属性值"), HttpStatus.OK);
		}
		if(infTask.getIsProject()==YesNo.No.getIntValue()&&infTask.getParentNodeId().equals(Constants.RootNode)) {
			return new ResponseEntity<ResultMsg>(new ResultMsg(false, "上级任务/项目不能是项目列表！","上级任务/项目不能是项目列表！"), HttpStatus.OK);
		}
		ResultMsg resultMag = infTaskService.addTask(infTask, user);
		if (!resultMag.isSuccess()) {
			log.info("复制项目/任务失败");
			resultMag.setMsg("复制项目/任务失败");
			return new ResponseEntity<ResultMsg>(resultMag, HttpStatus.OK);
		}

		log.info("复制项目/任务成功", user.getLoginId(), user.getUsername(), getClientIp());
		resultMag.setMsg("复制项目/任务成功");
		return new ResponseEntity<ResultMsg>(resultMag, HttpStatus.OK);
	}
	/**
	 * 
	 * <p> 获取管理员用户首页任务数据统计  </p>  
	 * @return
	 * @return ResponseEntity<Map<String,List<Map<String,Object>>>>
	 * @throws  
	 * @author litao
	 * @date 2021年9月28日 
	 * @time 上午11:50:43
	 */

	@Operation(summary ="项目数量")
	@GetMapping("/infTask/projectCount")
	public ResponseEntity<Map<String,List<Map<String,Object>>>> getProjectCount(){
		
		return new ResponseEntity(infTaskService.getProjectCount(),HttpStatus.OK);
	}
}
